﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DuAnNH.Fd_class;

namespace DuAnNH
{
    public partial class Frm_GhepBan : Form
    {
        public Frm_GhepBan()
        {
            InitializeComponent();
        }
        string BGoc = "";
        string BGhep = "";
        int lv_ghep = 0;
        private void Frm_GhepBan_Load(object sender, EventArgs e)
        {
            Load_LV_BGoc();
            Load_LV_BGhep();
        }
        public void Load_LV_BGoc()
        {
            DataTable tb = new DataTable();
            tb = Cls_Ban.MA_BanGoc();
            load_banC(tb);
        }
        public void load_banC(DataTable tb)
        {
            LV_Bgoc.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = tb.Rows[i][0].ToString();
                if (tb.Rows[i][2].ToString() == "1")
                    item.ImageIndex = 1;
                else
                {
                    if (tb.Rows[i][2].ToString() == "3")
                        item.ImageIndex = 2;
                }
                LV_Bgoc.Items.Add(item);
            }
        }

        private void LV_Bgoc_MouseClick(object sender, MouseEventArgs e)
        {
            BGoc = LV_Bgoc.FocusedItem.SubItems[0].Text;
            Lbl_Goc.Text = BGoc;
            //btt_add_td.Enabled = true;
            //Btn_Update.Enabled = true;
            //ahdv = 1;
            //aht = 1;
            //string MB = LV_Ban.FocusedItem.SubItems[0].Text;
            string st = string.Format("UPDATE Ban SET TinhTrang=1 WHERE TinhTrang=3");
            SqlCommand comd = new SqlCommand(st, Coso.con);
            comd.ExecuteNonQuery();
            string str = string.Format("UPDATE Ban SET TinhTrang=3 WHERE MaBan='{0}'", BGoc);
            SqlCommand com = new SqlCommand(str, Coso.con);
            com.ExecuteNonQuery();
            lv_ghep = 1;
            Load_LV_BGoc();
        }
        public void Load_LV_BGhep()
        {
            DataTable tb = new DataTable();
            tb = Cls_Ban.MA_BanGhep();
            load_banP(tb);
        }
        public void load_banP(DataTable tb)
        {
            LV_Bghep.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = tb.Rows[i][0].ToString();
                if (tb.Rows[i][2].ToString() == "0")
                    item.ImageIndex = 0;
                else
                {
                    if (tb.Rows[i][2].ToString() == "4")
                        item.ImageIndex = 2;
                }
                LV_Bghep.Items.Add(item);
            }
        }
        private void Frm_GhepBan_FormClosed(object sender, FormClosedEventArgs e)
        {
            string st = string.Format("UPDATE Ban SET TinhTrang=1 WHERE TinhTrang=3");
            SqlCommand com = new SqlCommand(st, Coso.con);
            com.ExecuteNonQuery();
            string str = string.Format("UPDATE Ban SET TinhTrang=0 WHERE TinhTrang=4");
            SqlCommand comd = new SqlCommand(str, Coso.con);
            comd.ExecuteNonQuery();
        }

        private void LV_Bghep_MouseClick(object sender, MouseEventArgs e)
        {
            //if (lv_ghep == 1)
            //{
                BGhep = LV_Bghep.FocusedItem.SubItems[0].Text;
                Lbl_Ghep.Text = BGhep;
                string st = string.Format("UPDATE Ban SET TinhTrang=0 WHERE TinhTrang=4");
                SqlCommand comd = new SqlCommand(st, Coso.con);
                comd.ExecuteNonQuery();
                string str = string.Format("UPDATE Ban SET TinhTrang=4 WHERE MaBan='{0}'", BGhep);
                SqlCommand com = new SqlCommand(str, Coso.con);
                com.ExecuteNonQuery();
                Load_LV_BGhep();
                lv_ghep = 0;
            //}
        }

        private void Btn_G_Click(object sender, EventArgs e)
        {
            if (Lbl_Goc.Text != "..." && Lbl_Ghep.Text != "...")
            {
                string str = string.Format("UPDATE Ban SET TinhTrang=5, GhepBan='{0}' WHERE MaBan='{1}'", Lbl_Goc.Text, Lbl_Ghep.Text);
                SqlCommand com = new SqlCommand(str, Coso.con);
                com.ExecuteNonQuery();
                Load_LV_BGhep();
                Lbl_Ghep.Text = "...";
            }
            else
                MessageBox.Show("Vui Lòng Chọn Bàn ^^!");
        }
    }
}
